﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
using System.Drawing;

namespace Common
{
    public class ImageHelper
    {
        // Methods
        public static bool MakeSmallImage(string srcFileFullName, string disFileFullName, Size smallSize)
        {
            Image srcImage = null;
            try
            {
                srcImage = Image.FromFile(srcFileFullName);
            }
            catch
            {
                return false;
            }
            return MakeSmallImage(srcImage, disFileFullName, smallSize, 0x4b);
        }

        private static bool MakeSmallImage(Image srcImage, string disFileFullName, Size smallSize, int quality)
        {
            bool flag = true;
            Bitmap image = null;
            try
            {
                ImageFormat rawFormat = srcImage.RawFormat;
                image = new Bitmap(srcImage, smallSize);
                Graphics graphics = Graphics.FromImage(image);
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(srcImage, new Rectangle(0, 0, smallSize.Width, smallSize.Height), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel);
                graphics.Dispose();
                EncoderParameters encoderParams = new EncoderParameters();
                long[] numArray = new long[] { (long)quality };
                EncoderParameter parameter = new EncoderParameter(Encoder.Quality, numArray);
                encoderParams.Param[0] = parameter;
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo encoder = null;
                for (int i = 0; i < imageEncoders.Length; i++)
                {
                    if (imageEncoders[i].FormatDescription.Equals("JPEG"))
                    {
                        encoder = imageEncoders[i];
                        break;
                    }
                }
                if (encoder != null)
                {
                    image.Save(disFileFullName, encoder, encoderParams);
                    return flag;
                }
                image.Save(disFileFullName, rawFormat);
                return flag;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                if (srcImage != null)
                {
                    srcImage.Dispose();
                }
                if (image != null)
                {
                    image.Dispose();
                }
            }
            return flag;
        }

        public static bool MakeSmallImage(string srcFileFullName, string disFileFullName, Size smallSize, int quality)
        {
            Image srcImage = null;
            try
            {
                srcImage = Image.FromFile(srcFileFullName);
            }
            catch
            {
                return false;
            }
            return MakeSmallImage(srcImage, disFileFullName, smallSize, quality);
        }

        public static bool MakeSmallImage(string srcFileFullName, string disFileFullName, int maxSideLength, LimitSideMode limitMode)
        {
            Image srcImage = null;
            try
            {
                srcImage = Image.FromFile(srcFileFullName);
            }
            catch
            {
                return false;
            }
            decimal num = 1M;
            switch (limitMode)
            {
                case LimitSideMode.Width:
                    if (srcImage.Width > maxSideLength)
                    {
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Width;
                        break;
                    }
                    File.Copy(srcFileFullName, disFileFullName);
                    return true;

                case LimitSideMode.Height:
                    if (srcImage.Height > maxSideLength)
                    {
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Height;
                        break;
                    }
                    File.Copy(srcFileFullName, disFileFullName);
                    return true;

                case LimitSideMode.Auto:
                    if (srcImage.Width < srcImage.Height)
                    {
                        if (srcImage.Height <= maxSideLength)
                        {
                            File.Copy(srcFileFullName, disFileFullName);
                            return true;
                        }
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Height;
                        break;
                    }
                    if (srcImage.Width > maxSideLength)
                    {
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Width;
                        break;
                    }
                    File.Copy(srcFileFullName, disFileFullName);
                    return true;
            }
            Size smallSize = new Size(Convert.ToInt32((decimal)(srcImage.Width * num)), Convert.ToInt32((decimal)(srcImage.Height * num)));
            return MakeSmallImage(srcImage, disFileFullName, smallSize, 0x4b);
        }

        public static bool MakeSmallImage(string srcFileFullName, string disFileFullName, int maxSideLength, LimitSideMode limitMode, int quality)
        {
            Image srcImage = null;
            try
            {
                srcImage = Image.FromFile(srcFileFullName);
            }
            catch
            {
                return false;
            }
            decimal num = 1M;
            switch (limitMode)
            {
                case LimitSideMode.Width:
                    if (srcImage.Width > maxSideLength)
                    {
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Width;
                        break;
                    }
                    File.Copy(srcFileFullName, disFileFullName);
                    return true;

                case LimitSideMode.Height:
                    if (srcImage.Height > maxSideLength)
                    {
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Height;
                        break;
                    }
                    File.Copy(srcFileFullName, disFileFullName);
                    return true;

                case LimitSideMode.Auto:
                    if (srcImage.Width < srcImage.Height)
                    {
                        if (srcImage.Height <= maxSideLength)
                        {
                            File.Copy(srcFileFullName, disFileFullName);
                            return true;
                        }
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Height;
                        break;
                    }
                    if (srcImage.Width > maxSideLength)
                    {
                        num = Convert.ToDecimal(maxSideLength) / srcImage.Width;
                        break;
                    }
                    File.Copy(srcFileFullName, disFileFullName);
                    return true;
            }
            Size smallSize = new Size(Convert.ToInt32((decimal)(srcImage.Width * num)), Convert.ToInt32((decimal)(srcImage.Height * num)));
            return MakeSmallImage(srcImage, disFileFullName, smallSize, quality);
        }

        public static bool Mark(string srcPicFileFullName, string markPicFileFullName, string outPicFileFullName, int rightSpace, int bottomSpace, int lucencyPercent)
        {
            bool flag = false;
            Image image = null;
            Image image2 = null;
            Graphics graphics = null;
            try
            {
                image = Image.FromFile(srcPicFileFullName);
                image2 = Image.FromFile(markPicFileFullName);
                graphics = Graphics.FromImage(image);
                int x = (image.Width - rightSpace) - image2.Width;
                int y = (image.Height - bottomSpace) - image2.Height;
                float[][] numArray2 = new float[5][];
                float[] numArray3 = new float[5];
                numArray3[0] = 1f;
                numArray2[0] = numArray3;
                float[] numArray4 = new float[5];
                numArray4[1] = 1f;
                numArray2[1] = numArray4;
                float[] numArray5 = new float[5];
                numArray5[2] = 1f;
                numArray2[2] = numArray5;
                float[] numArray6 = new float[5];
                numArray6[3] = ((float)lucencyPercent) / 100f;
                numArray2[3] = numArray6;
                float[] numArray7 = new float[5];
                numArray7[4] = 1f;
                numArray2[4] = numArray7;
                float[][] newColorMatrix = numArray2;
                ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                graphics.DrawImage(image2, new Rectangle(x, y, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttr);
                FileInfo info = new FileInfo(srcPicFileFullName);
                ImageFormat gif = ImageFormat.Gif;
                switch (info.Extension.ToLower())
                {
                    case ".jpg":
                        gif = ImageFormat.Jpeg;
                        break;

                    case ".gif":
                        gif = ImageFormat.Gif;
                        break;

                    case ".png":
                        gif = ImageFormat.Png;
                        break;

                    case ".bmp":
                        gif = ImageFormat.Bmp;
                        break;

                    case ".tif":
                        gif = ImageFormat.Tiff;
                        break;

                    case ".wmf":
                        gif = ImageFormat.Wmf;
                        break;

                    case ".ico":
                        gif = ImageFormat.Icon;
                        break;
                }
                MemoryStream stream = new MemoryStream();
                image.Save(stream, gif);
                byte[] buffer = stream.ToArray();
                image.Dispose();
                image2.Dispose();
                graphics.Dispose();
                FileStream stream2 = null;
                stream2 = new FileStream(outPicFileFullName, FileMode.Create, FileAccess.Write);
                if (stream2 != null)
                {
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.Close();
                }
                flag = true;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                try
                {
                    image2.Dispose();
                    image.Dispose();
                    graphics.Dispose();
                }
                catch
                {
                }
            }
            return flag;
        }

        public static bool Mark(string srcPicFileFullName, string markPicFileFullName, string outPicFileFullName, int rightSpacePercent, int bottomSpacePercnet, int maskWidthSizePercent, int lucencyPercent)
        {
            bool flag = false;
            Image image = null;
            Image image2 = null;
            Graphics graphics = null;
            try
            {
                image = Image.FromFile(srcPicFileFullName);
                image2 = Image.FromFile(markPicFileFullName);
                graphics = Graphics.FromImage(image);
                int width = Convert.ToInt32((float)(image.Width * (((float)maskWidthSizePercent) / 100f)));
                int height = Convert.ToInt32((float)(image2.Height * (((float)width) / ((float)image2.Width))));
                int num3 = Convert.ToInt32((float)(image.Width * (((float)rightSpacePercent) / 100f))) + width;
                int num4 = Convert.ToInt32((float)(image.Height * (((float)bottomSpacePercnet) / 100f))) + height;
                int x = image.Width - num3;
                int y = image.Height - num4;
                float[][] numArray2 = new float[5][];
                float[] numArray3 = new float[5];
                numArray3[0] = 1f;
                numArray2[0] = numArray3;
                float[] numArray4 = new float[5];
                numArray4[1] = 1f;
                numArray2[1] = numArray4;
                float[] numArray5 = new float[5];
                numArray5[2] = 1f;
                numArray2[2] = numArray5;
                float[] numArray6 = new float[5];
                numArray6[3] = ((float)lucencyPercent) / 100f;
                numArray2[3] = numArray6;
                float[] numArray7 = new float[5];
                numArray7[4] = 1f;
                numArray2[4] = numArray7;
                float[][] newColorMatrix = numArray2;
                ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                graphics.DrawImage(image2, new Rectangle(x, y, width, height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttr);
                FileInfo info = new FileInfo(srcPicFileFullName);
                ImageFormat gif = ImageFormat.Gif;
                switch (info.Extension.ToLower())
                {
                    case ".jpg":
                        gif = ImageFormat.Jpeg;
                        break;

                    case ".gif":
                        gif = ImageFormat.Gif;
                        break;

                    case ".png":
                        gif = ImageFormat.Png;
                        break;

                    case ".bmp":
                        gif = ImageFormat.Bmp;
                        break;

                    case ".tif":
                        gif = ImageFormat.Tiff;
                        break;

                    case ".wmf":
                        gif = ImageFormat.Wmf;
                        break;

                    case ".ico":
                        gif = ImageFormat.Icon;
                        break;
                }
                MemoryStream stream = new MemoryStream();
                image.Save(stream, gif);
                byte[] buffer = stream.ToArray();
                image.Dispose();
                image2.Dispose();
                graphics.Dispose();
                FileStream stream2 = null;
                stream2 = new FileStream(outPicFileFullName, FileMode.Create, FileAccess.Write);
                if (stream2 != null)
                {
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.Close();
                }
                flag = true;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                try
                {
                    image2.Dispose();
                    image.Dispose();
                    graphics.Dispose();
                }
                catch
                {
                }
            }
            return flag;
        }

        // Nested Types
        public enum LimitSideMode
        {
            Width,
            Height,
            Auto
        }
    }

}
